// 用户认证相关 API

import request from './index';
import type { LoginRequest, LoginResponse, RegisterRequest, RegisterResponse } from '@/types/user';
import type { ApiResponse } from '@/types/api';
import { mockLogin, mockRegister, mockLogout } from '@/mock/auth';

const useMock = import.meta.env.VITE_USE_MOCK === 'true';

/**
 * 用户登录
 */
export const login = async (data: LoginRequest): Promise<ApiResponse<LoginResponse>> => {
  if (useMock) {
    return mockLogin(data);
  }
  return request.post('/auth/login', data);
};

/**
 * 用户注册
 */
export const register = async (data: RegisterRequest): Promise<ApiResponse<RegisterResponse>> => {
  if (useMock) {
    return mockRegister(data);
  }
  return request.post('/auth/register', data);
};

/**
 * 用户登出
 */
export const logout = async (): Promise<ApiResponse<null>> => {
  if (useMock) {
    return mockLogout();
  }
  return request.post('/auth/logout');
};